﻿using Microsoft.AspNetCore.Mvc;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using System;
using Volo.Abp.Application.Services;
using System.Linq;
using EMS.system.Entityies.MasterData.MaterialTypeModel;
using System.Collections.Generic;
using EMS.system.IEntityAppService.MasterData.MaterialTypeIService.MaterialTypeDTO;
using EMS.system.IEntityAppService.MasterData.MaterialTypeIService;
using CSRedis;

namespace EMS.system.Web.Controllers.MasterData.MaterialTypes
{
    public class MaterialTypeController : Controller
    {
        private readonly IMaterialTypeAppService _service;

        public MaterialTypeController(IMaterialTypeAppService service)
        {
            _service = service;
        }


        public IActionResult Index()
        {
            return View();
        }


        public IActionResult InsterView(int id, int pid)
        {
            ViewBag.id = id;
            ViewBag.pid = pid;
            return View();
        }

        /// <summary>
        /// 物料类型新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> MaterialTypeInsert(MaterialTypeInsertDTO model)
        {
            var res = await _service.MaterialTypeInsert(model);
            return res;
        }


        public IActionResult GetMaterialTypeView()
        {
            return View();
        }

        /// <summary>
        /// 获取一级物料类型
        /// </summary>
        /// <param name="materialTypeName"></param>
        /// <returns></returns>
        public async Task<JsonResult> GetMaterialType()
        {
            var list = await _service.GetMaterialType();
            return Json(new { data = list,count = list.Count(),code=0 });
        }

        /// <summary>
        /// 物料类型删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> MaterialTypeDelete(int Id)
        {
            await _service.MaterialTypeDelete(Id);
            return 1;
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetAll()
        {
            var typeList = await _service.GetAll();
            return Json(typeList);
        }


        public IActionResult UpdateView(int id,int pid)
        {
            ViewBag.id = id;
            ViewBag.pid = pid;
            return View("InsterView");
        }

        /// <summary>
        /// 获取单个物料类型
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<MaterialType> GetSingMaterialType(int id)
        {
            var typeModel = await _service.GetSingMaterialType(id);
            return typeModel;
        }

        /// <summary>
        /// 物料类型编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> MaterialTypeUpdate(MaterialTypeUpdateDTO model)
        {
            var data = await _service.MaterialTypeUpdate(model);
            return data;
        }
    }
}
